<?php
namespace Admin\Service;

use Think\Service\BasicService;

class YZOrderService extends BasicService {
    public function insert($model) {
        return $this->db->add($model);
    }

    public function remove($condition) {
        return $this->db->where($condition)->delete();
    }

    public function update($where, $model = array()) {
        return $this->db->where($where)->save($model);
    }

    public function find($condition) {
        return $this->db->find($condition);
    }

    public function query($condition) {
        $data = $this->db->where($condition)->order('created desc')->select();
        foreach ($data as $k => $v) {
            $this->db = M('yz_order_info');
            $datas = $this->db->where(array('oid' => $v['tid']))->select();
            $data[$k]['goodsdata'] = $datas;
            switch ($v['status']) {
                case 'TRADE_NO_CREATE_PAY':
                    $data[$k]['status'] = '没有创建支付交易';
                    break;
                case 'WAIT_BUYER_PAY':
                    $data[$k]['status'] = '等待买家付款';
                    break;
                case 'WAIT_GROUP':
                    $data[$k]['status'] = '等待成团';
                    break;
                case 'WAIT_SELLER_SEND_GOODS':
                    $data[$k]['status'] = '等待卖家发货';
                    break;
                case 'WAIT_BUYER_CONFIRM_GOODS':
                    $data[$k]['status'] = '等待买家确认收货';
                    break;
                case 'TRADE_BUYER_SIGNED':
                    $data[$k]['status'] = '买家已签收';
                    break;
                case 'TRADE_CLOSED':
                    $data[$k]['status'] = '付款以后用户退款成功，交易自动关闭';
                    break;
                case 'ALL_WAIT_PAY':
                    $data[$k]['status'] = '包含：WAIT_BUYER_PAY、TRADE_NO_CREATE_PAY';
                    break;
                case 'ALL_CLOSED':
                    $data[$k]['status'] = '所有关闭订单';
                    break;
                default:
                    $data[$k]['status'] = '其他';
            }
        }
        return $data;
    }

    public function AddAll($model, $model1, $where) {
        //无数据更新
        if (!$model) {
            $this->db->commit();
            return 2;
        }
        $this->db->startTrans();
        $Hdata = $this->db->where($where)->find();
        $DELOK = 1;
        $DELINFO = 1;
        if ($Hdata) {
            $DELOK = $this->db->where($where)->delete();
            $this->db = M('yz_order_info');
            $where1['t_yz_order.status'] = $where['status'];
            $DELdata = $this->db->field('t_yz_order_info.id')
                ->join('t_yz_order on t_yz_order_info.oid = t_yz_order.tid')
                ->where($where1)->select();
            $where2 = $where = 'id in(' . implode(',', $DELdata) . ')';
            dump($DELdata);

            dump($where2);
            dump($this->db->_sql());
            die();
            $DELINFO = $this->db->where($where2)->delete();

            $this->db = M('yz_order');
        }

        if ($DELOK && $DELINFO) {
            foreach ($model as $k => $v) {
                $ADDOK = $this->db->addAll($v);
                if (!$ADDOK) {
                    $this->db->rollback();
                    return 0;
                }
            }
            foreach ($model1 as $k1 => $v1) {
                $ADDOK1 = $this->db->addAll($v1);
                if (!$ADDOK1) {
                    $this->db->rollback();
                    return 0;
                }
            }
            $this->db->commit();
            return 1;
        } else {
            $this->db->rollback();
            return 0;
        }

    }

    /*
     * 拼接订单数据显示
     */
    public function GetOrderData($OrderData =array(),$OrderDataInfo = array()){
        foreach ($OrderData as $k => $v) {
            $ISOK = 1;
            $datas = array();
            foreach($OrderDataInfo as $k1 =>$v1){
                IF($v1['oid'] == $v['tid']){
                    $CODE = M('goods_code')->where(array('SHOP_CODE'=>$v1['alias']))->find();

                    if($CODE){
                        $v1['CODE'] = $CODE['ERP_CODE'];
                    }else{
                        $ISOK = 0;
                        $v1['CODE'] = 0;
                    }

                    $datas[] = $v1;
                }
            }
            $OrderData[$k]['goodsdata'] = $datas;
            $OrderData[$k]['red'] = $ISOK ?0:1;
            $YZ_ORDER_STATE = C('YZ_ORDER_STATE');
            $OrderData[$k]['status'] = $YZ_ORDER_STATE[$OrderData[$k]['status']];
        }
        return $OrderData;
    }

    /*
     * 校验订单表数据
     * 内码对应性
     */
    public function checkOrder($OrderData){

    }

}

?>